package com.gree.flowable.app.tools.utils;

import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.usermodel.XSSFComment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：180849
 * @date ：Created in 2021/5/11 14:03
 * @description：sax非用户这模式
 * @modified By：
 * @version: 1.0
 */
public class SheetHandler implements XSSFSheetXMLHandler.SheetContentsHandler{

    /**
     * 数据表对应的表头
     */
    private List<Map<String,String>> resultColumnList;

    /**
     * 封装实体对象
     */
    private Map<String, Object> map;

    /**
     * 字段格式类型为时间
     */
    private Map<String,String> dataTypeMap;


    public List<Map<String, Object>> getMapsList() {
        return mapsList;
    }

    /**
     * 实体对象集合
     */
    private List<Map<String, Object>> mapsList = new ArrayList<>(MAX_EMPLOYEE);

    /**
     * 集合最大容量
     */
    private static final int MAX_EMPLOYEE = 1024;

    /**
     * 表头对应的Key的集合
     */
    private Map<String, String> keyMap;

    /**
     * 第几次插入数据，初始值为1
     */
    private int times = 1;

    /**
     * 总数据量
     */
    private int allCount = 0;

    public SheetHandler(List<Map<String,String>> resultColumnList,Map<String,String> dataTypeMap) throws Exception{
        this.dataTypeMap = dataTypeMap;
        this.resultColumnList = resultColumnList;
    }

    /**
     * 当开始解析某一行的时候出发
     *
     * @param rowNum    行号
     */
    @Override
    public void startRow(int rowNum) {
        if (rowNum > 0){
            map = new HashMap<>(16);
        } else {
            keyMap = new HashMap<>(16);
        }
    }

    /**
     * 当结束解析某一行的时候出发
     *
     * @param rowNum 行号
     */
    @Override
    public void endRow(int rowNum) {
        if (map != null) {
            mapsList.add(map);
            allCount++;
        }
    }

    /**
     * A cell, with the given formatted value (may be null),
     * and possibly a comment (may be null), was encountered.
     * <p>
     * Sheets that have missing or empty cells may result in
     * sparse calls to <code>cell</code>. See the code in
     * <code>src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java</code>
     * for an example of how to handle this scenario.
     *
     * @param cellName     单元格名称
     * @param value    值
     * @param comment           批注
     */
    @Override
    public void cell(String cellName, String value, XSSFComment comment) {
        int length = allCount + 1;
        String prefix = cellName.substring(0, cellName.length() - length);
        long timeValue = 0;
//        if (!cellName.equals("A1")) {
            if (map != null) {
                if (keyMap.get(prefix).equals(dataTypeMap.get("field"))){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
                    Date date = null;
                    try {
                        date = simpleDateFormat.parse(value);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    timeValue = date.getTime() / 1000;

                    map.put(keyMap.get(prefix), timeValue);
                } else {
                    map.put(keyMap.get(prefix), value);
                }
            } else {
                for (Map<String, String> columnMap : resultColumnList) {
//                    String[] split = value.split("\\(");
//                    String field = split[1].substring(0,split[1].length()-1);
                    String field = value.substring(columnMap.get("name").length());
                    if (columnMap.get("field").equals(field)){
                        keyMap.put(prefix, columnMap.get("field"));
                        break;
                    }
                }

            }
//        }
    }
}
